<?php
/**
 * Created by PhpStorm.
 * User: 程序猴
 * Date: 2018/4/8
 *  Q Q: 505413
 * Time: 13:52
 */

namespace app\admin\controller;
use app\admin\controller\Base;
use think\Db;
use app\admin\model\MenuModel;
use think\Controller;
class Menu extends Base
{
    //后台菜单展示
    public function index()
    {
        $date = Db::name('menu')->order('level asc , sort asc')->select();
        $list = $this->tree($date);
        foreach ($list as $k => $v){
            $list[$k]['checked'] = $v['status'] ? 'checked' : '';
            $list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
            $str = "";
            if ($v['level'] > 0) {
                $str = str_repeat("&emsp;│", $v['level'] );
            }
            if ($v['level'] != 0) {
                if (isset($list[$k + 1]) && $list[$k + 1]['level'] >= $list[$k]['level']) {
                    $list[$k]['title'] = $str . "&emsp;├─ " . $v['title'];
                } else {
                    $list[$k]['title'] = $str . "&emsp;└─ " . $v['title'];
                }
            } else {
                $list[$k]['title'] = "&emsp;├─ " . $v['title'];
            }
        }
        $this->assign('list', $list);
        return $this->fetch();
    }
    //菜单排序
    public function sort()
    { 
        if (request()->isAjax()){
            $model  =   Db::name('menu');
            $data   =   input('post.');
            if(empty($data['id']) || empty($data['sort']) || count($data['id'])!==count($data['sort'])){
                return json(['code' => 0, 'msg' => '参数错误']);
            }
            for ($i=0;$i<count($data['id']);$i++){
                $res = $model->where(array('id' => $data['id'][$i]))->setField(array('sort' => $data['sort'][$i]));
                if($res === false)
                    return json(['code' => 0, 'msg' => '排序更新失败']);
            }
            return json(['code' => 1, 'msg' => '排序更新成功']);
        }
    }
    //修改开启状态
    public function status_edit()
    {
        $id = input('param.id');
        $status = Db::name('menu')->where('id',$id)->value('status');//判断当前状态情况
        if($status==1)
        {
            $flag = Db::name('menu')->where('id',$id)->setField(['status'=>0]);
            return json(['code' => -1, 'url' => 'link/index', 'msg' => '已禁止']);
        }
        else
        {
            $flag = Db::name('menu')->where('id',$id)->setField(['status'=>1]);
            return json(['code' => 1, 'url' => 'link/index', 'msg' => '已开启']);
        }
    }
    public function tree($data, $pid = 0){
        $array = array();
        foreach ($data as $val){
            if($pid == $val['pid']){
                $array[] = $val;
                $childen = $this->tree($data, $val['id']);
                if(!empty($childen)){
                    $array = array_merge($array, $childen);
                }
            }
        }
        return $array;
    }
    //添加菜单
    public function menuAdd() {
        if(request()->isAjax()) {
            $param = input('post.');
            $param['status'] = $param['status'] == 'on' ? 1 : 0;
            $param['spread'] = 0;
            $param['create_time'] = time();
            $param['update_time'] = time();
            $Menu = new MenuModel();
            return $Menu->insertMenu($param);
        } else {
            $pid = input('get.pid');
            if(!empty($pid)){
                $parent =  Db::name('menu')->where(array('id' => $pid))->field('id,level,title')->find();
            } else {
                $parent = array('id' => 0, 'level' => 0, 'title' => '顶级菜单');
            }
            $this->assign('parent', $parent);
            return $this->fetch();
        }
       
    }
    //编辑菜单
    public function menuEdit(){
        $id = input('id');
        if(request()->isAjax()){
            $param = input('post.');
            if(!empty($param['status'])){
                if($param['status'] == 'on') {
                    $param['status'] = '1';
                }
            }else{
                $param['status'] = '0';
            }
            $param['spread'] = 0;
            $param['create_time'] = time();
            $param['update_time'] = time();

            $data = Db::name("menu")->where(array('id'=>$id))->update($param);
            if(false === $data){
                return json(['code' => -1, 'url' => 'link/index', 'msg' => '修改菜单失败']);
            }else{
                return json(['code' => 1, 'url' => 'link/index', 'msg' => '修改菜单成功']);
            }
        }else{
            if(!empty($id)){
                $parent =  Db::name('menu')->where(array('id' => $id))->find();
            }
            $this->assign('parent', $parent);
        }
        return $this->fetch();
    }
    //删除菜单
    public function menuDel()
    {
        $id = input('id');
        $res = Db::name('menu')->where(array('id'=>$id))->delete();
        if($res){
            return json(['code' => 1, 'url' => 'menu/index', 'msg' => '删除菜单成功']);
        }else{
            return json(['code' => -1, 'url' => 'menu/index', 'msg' => '删除菜单失败，请重试']);
        }
    }
}


